package com.lipaluma.example.clients.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.joda.time.DateTime;

import com.lipaluma.example.clients.model.Account;
import com.lipaluma.example.clients.model.Invoice;


public class IInvoiceDAOImpl implements IInvoiceDAOCustom {

	@PersistenceContext(unitName="ClientRefServicesPU")
	private EntityManager entityManager;

	@Override
	public List<Invoice> getInvoicesForTheMonth(int year, int month) {
		DateTime start = new DateTime().withYear(year).withMonthOfYear(month).withDayOfMonth(01);
		DateTime end = new DateTime().withYear(year).withMonthOfYear(month).dayOfMonth().withMaximumValue();
		Query query = entityManager.createNamedQuery("Invoices.findForEcheance");
		query.setParameter("start", start.toDate());
		query.setParameter("end", end.toDate());
		return query.getResultList();
	}
	
	@Override
	public List<Invoice> getInvoicesForAccount(Long idAccount) {
		Account account = new Account();
		account.setId(idAccount);
		Query query = entityManager.createNamedQuery("Invoices.findForAccount");
		query.setParameter("id", account);
		return query.getResultList();
	}

}
